package com.nip.tranning.Activity;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nip.trainning.Common.Constant;
import com.nip.trainning.Common.Utilities;
import com.nip.trainning.DataTransferObject.PersonDataTable;
import com.nip.trainning.DataTransferObject.PersonEntity;
import com.nip.trainning.SQLiteProject.R;

public class MainActivity extends Activity {

	private Button btnAdd;
	private PersonDataTable personTable;
	private AlertDialog.Builder b;
	private EditText edName;
	private EditText edPhone;
	private PersonEntity person;
	private Button btnShow;
	private String path;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);

		File file = Utilities.CreateFolderInSdCard();
		
		if (file != null) {
			path = file.getPath();
		} else {
			path = "";
		}

		personTable = new PersonDataTable(this, path);

		edName = (EditText) findViewById(R.id.edName);
		edPhone = (EditText) findViewById(R.id.edPhone);
		btnShow = (Button)findViewById(R.id.btnShow);
		btnAdd = (Button) findViewById(R.id.btnAdd);
		b = new AlertDialog.Builder(MainActivity.this);
		b.setTitle("NIPTeam");
		b.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});

		btnAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String name = edName.getText().toString();
				String phone = edPhone.getText().toString();
				person = new PersonEntity();
				if (name == null || name.trim().equals("")) {
					b.setMessage("Name can not be null");
					b.show();
					edName.setText("");
					edName.setFocusable(true);
					return;
				}
				if (phone == null || phone.trim().equals("")) {
					b.setMessage("Phone can not be null");
					b.show();
					edPhone.setText("");
					edPhone.setFocusable(true);
					return;
				}

				person.setPersonName(name);
				person.setPhoneNumber(phone);
				
				try{
					personTable.InsertNewPerson(person);
					b.setMessage("Add " + name + " Success !");
					b.show();
					
				}catch(Exception e)
				{
					b.setMessage(e.getMessage());
					b.show();
				}
			}
		});
		
		btnShow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplication(), ShowPeopleActivity.class);
				ArrayList<PersonEntity> listPerson = (ArrayList<PersonEntity>) personTable.GetAllPersonInfomation();
				Bundle bundle = new Bundle();
				bundle.putSerializable(Constant.INTENT_CODE_SEND_LIST_PEOPLE, listPerson);
				bundle.putString(Constant.INTENT_CODE_SEND_PATH, path);
				i.putExtras(bundle);
				try {
					startActivity(i);
					
				} catch (Exception e) {
					Log.w("ERROR", e.getMessage());
				}
				
			}
		});

	}
}
